<template>
    <div>
        <my-list ref="list_folder" :url="folder_url"
                 :page-size="pageSize" :store="true"
                 style="width: 100%;">
            <el-table-column type="index" clign="center" width="50"></el-table-column>
            <el-table-column align="center" label="前缀" prop="start"></el-table-column>
            <el-table-column align="center" label="文件夹" prop="folder"></el-table-column>
            <el-table-column label="操作" align="center" width="180" v-if="jv.main.user.isAdmin">
                <template slot-scope="scope">
                    <el-button @click="folder_row= Object.assign({},scope.row);show_folder=true" size="small">
                        编辑
                    </el-button>
                    <el-button @click="delete_folder_click(scope.row)" size="small">
                        删除
                    </el-button>
                </template>
            </el-table-column>

            <template #query-button>
                <el-button @click="folder_row = {};show_folder=true;" type="primary"  v-if="jv.main.user.isAdmin">添加规则</el-button>
            </template>
        </my-list>
        <el-dialog :visible.sync="show_folder" title="文件件规则" ref="folder">
            <kv label="文件名前缀">
                <el-input v-model="folder_row.start" chk="*"></el-input>
            </kv>
            <kv label="文件夹">
                <div>
                    <el-input v-model="folder_row.folder" chk="*"></el-input>
                </div>
            </kv>

            <el-button type="primary" @click="save_folder_click"  v-if="jv.main.user.isAdmin">保存</el-button>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        components: {},
        props: {
            id: {type: String, default: ""}
        },
        watch: {
            id: {
                immediate: true, handler(v) {
                    if (v) {
                        this.folder_url = "/site/b-site-folder/list?site_id=" + v;
                    }
                }
            }
        },
        data() {
            return {
                pageSize: 10,
                folder_url: "",
                show_folder: false,
                folder_row: {},
            }
        },
        mounted() {

        },
        methods: {
            save_folder_click() {
                if (this.$refs.folder.chk() == false) {
                    return;
                }

                this.folder_row.site_id = this.id;
                this.folder_row.folder = this.folder_row.folder.trimWith("/");
                this.$http.post("/site/b-site-folder/save", this.folder_row).then(res => {
                    this.$refs.list_folder.loadData();
                    this.show_folder = false;
                })
            },
            delete_folder_click(row) {
                var rowId = row.id;
                var rowName = row.name;
                jv.confirm('确认删除 ' + rowName + ' 吗？').then(res => {
                    this.$http.post("/site/b_site_folder/delete/" + rowId).then(res => {
                        this.loadData();
                    })
                });
            },
        }
    }
</script>
